/*
    Copyright 2010 Northbranchlogic, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.runjrun.gui;

import com.norbl.runjrun.*;
import javax.swing.*;
import java.awt.event.*;
import com.norbl.util.*;

/**
 *
 * @author Barnet Wagman
 */
public class MonitorGui extends javax.swing.JFrame
    implements ActionListener {

    StateTableModels stateTableModels;
    Actions actions;

    enum Command {
        EXIT, EDIT_PARAMS, UPDATE, ABOUT
    }

    private String awsAccessID;
    private String awsSecretKey;
    private String bucketName;
    private Monitor monitor;
    
    /** Creates new form MonitorGui */
    public MonitorGui(StateTableModels stateTableModels, Actions actions,
                      String awsAccessID, String awsSecretKey,
                      String bucketName,
                      Monitor monitor) {
        this.stateTableModels = stateTableModels;
        this.actions = actions;       
        this.awsAccessID = awsAccessID;
        this.awsSecretKey = awsSecretKey;
        this.bucketName = bucketName;
        this.monitor = monitor;
    }

    public void startGui() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GuiUtil.setNBLIcon(MonitorGui.this);
                initComponents();
                setVisible(true);
            }
        });
    }

        /** This method handles only actions that directly relate to the
         *  gui. Actions that trigger operations on tasks and nodes
         *  are handled in {@link Actions}.
         *
         * @param ev
         */
    public void actionPerformed(ActionEvent ev) {

        String c = ev.getActionCommand();       
        Command cmd = Command.valueOf(c);

        switch(cmd) {
            case EXIT:  
                System.exit(0);
            case EDIT_PARAMS:
                editParams();
                break;
            case UPDATE:
                update();
                break;
            case ABOUT:
                showAbout();
                break;
            default:
        }
    }

    void editParams() {
        MonitorParamHandler h = new MonitorParamHandler();
        h.awsAccessID = awsAccessID;
        h.awsSecretKey = awsSecretKey;
        h.bucketName = bucketName;
        
        if ( h.getNewParams(this) ) {
            Monitor.launchWithGui();
            monitor.stopUpdater();
            this.dispose();            
        }
    }

    void update() {
        (new Thread(new Runnable() {
            public void run() {
                try {
                    TaskInProgressWindow tip =
                                    TaskInProgressWindow.run("RunJRun Monitor",
                                        "RunJRun Monitor",
                                        "Update in progress.");
                    monitor.update();
                    tip.dispose();
                }
                catch(Exception xxx) {
                    System.out.println(StringUtil.throwableToString(xxx));
                }
            }
        })).start();
    }

    void showAbout() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AboutGui ag = new AboutGui();
                GuiUtil.centerOnScreen(ag);
                GuiUtil.setNBLIcon(ag);
                ag.setVisible(true);
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taskSummaryTable = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        nodeTable = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        nodeSummaryTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        taskTable = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        updateMenuItem = new javax.swing.JMenuItem();
        editParamsMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        nodeCommandMenu = new javax.swing.JMenu();
        taskCommandMenu = new javax.swing.JMenu();
        helpMenu = new javax.swing.JMenu();
        aboutRJRMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("RunJRun Monitor");

        jPanel1.setBackground(java.awt.Color.white);

        taskSummaryTable.setModel(stateTableModels.taskSummaryTableModel);
        jScrollPane1.setViewportView(taskSummaryTable);

        nodeTable.setModel(stateTableModels.nodeStateTableModel);
        actions.nodeClickHandler.setJTable(nodeTable);
        jScrollPane2.setViewportView(nodeTable);

        nodeSummaryTable.setModel(stateTableModels.nodeSummaryTableModel);
        jScrollPane3.setViewportView(nodeSummaryTable);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 0, 16));
        jLabel1.setText("Task summary");

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 0, 16));
        jLabel2.setText("Node summary");

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 0, 16));
        jLabel3.setText("Nodes");

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/norbl/runjrun/gui/graphics/rjr_logo_small.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addContainerGap())
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 804, Short.MAX_VALUE)
                                .addComponent(jLabel2)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 558, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 804, Short.MAX_VALUE))
                            .addGap(24, 24, 24)))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(695, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addGap(34, 34, 34))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                .addGap(12, 12, 12)
                .addComponent(jLabel5)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Summaries, Nodes", jPanel1);

        jPanel2.setBackground(java.awt.Color.white);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/norbl/runjrun/gui/graphics/rjr_logo_small.jpg"))); // NOI18N

        taskTable.setModel(stateTableModels.taskStateTableModel);
        actions.taskClickHandler.setJTable(taskTable);
        jScrollPane5.setViewportView(taskTable);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 804, Short.MAX_VALUE)
                .addGap(24, 24, 24))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(695, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addGap(34, 34, 34))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addGap(12, 12, 12)
                .addComponent(jLabel4)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Tasks", jPanel2);

        fileMenu.setText("File");

        updateMenuItem.setText("Update");
        updateMenuItem.setToolTipText("Updates all contents of the monitor immediately.");
        updateMenuItem.setActionCommand(Command.UPDATE.toString());
        updateMenuItem.addActionListener(this);
        fileMenu.add(updateMenuItem);

        editParamsMenuItem.setText("Edit params");
        editParamsMenuItem.setActionCommand(Command.EDIT_PARAMS.toString());
        editParamsMenuItem.addActionListener(this);
        fileMenu.add(editParamsMenuItem);

        exitMenuItem.setText("Exit");
        exitMenuItem.setActionCommand(Command.EXIT.toString());
        exitMenuItem.addActionListener(this);
        fileMenu.add(exitMenuItem);

        jMenuBar1.add(fileMenu);
        jMenuBar1.add(Box.createHorizontalGlue());

        nodeCommandMenu.setText("Node commands");
        actions.addNodeOpActions(nodeCommandMenu);
        jMenuBar1.add(nodeCommandMenu);
        jMenuBar1.add(Box.createHorizontalGlue());

        taskCommandMenu.setText("Task commands");
        actions.addTaskOpActions(taskCommandMenu);
        jMenuBar1.add(taskCommandMenu);
        jMenuBar1.add(Box.createHorizontalGlue());

        helpMenu.setText("Help");

        aboutRJRMenuItem.setText("About RunJRun");
        aboutRJRMenuItem.setActionCommand(Command.ABOUT.toString());
        aboutRJRMenuItem.addActionListener(MonitorGui.this);
        helpMenu.add(aboutRJRMenuItem);

        jMenuBar1.add(helpMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 864, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MonitorGui(null,null,null,null,null,null).setVisible(true);
            }
        });
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutRJRMenuItem;
    private javax.swing.JMenuItem editParamsMenuItem;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JMenu nodeCommandMenu;
    private javax.swing.JTable nodeSummaryTable;
    private javax.swing.JTable nodeTable;
    private javax.swing.JMenu taskCommandMenu;
    private javax.swing.JTable taskSummaryTable;
    private javax.swing.JTable taskTable;
    private javax.swing.JMenuItem updateMenuItem;
    // End of variables declaration//GEN-END:variables

}
